/**
 * 
 */
$(function(){
    $(document).keyup(function (e) {
        if(e.which==13){
            $('#selectBtnEnter').click();
        }
    })
});
var path = '../';
var pageListUrl = path + 'advPlay/report';
var pageUrl = path + 'advPlay/';

//添加弹窗
var addPanel = null;
// ---------------交互代码begin-----------------
//本页面业务逻辑
var advPlay={
	checkLogic:function(dataObj){
		var result="";
		var isSubmit=true;
		//TODO 逻辑校验，去掉不必要的:
	    //检查表单是否存在错误
		$.each(dataObj, function(key) {
			if(key.indexOf('Err')!=-1 && dataObj[key].length>0){
				return false;
			}									 
		});
		return isSubmit;
	},
    /**
	 * 已code为key，从数组中移除元素
     * @param array
     * @param val
     */
	remove:function(array,val){
		var newArray = [];
		$.each(array,function (index) {
			if(array[index].code != val.code){
				newArray.push(array[index]);
			}
        })
		return newArray;
	}
};

//绑定列表List---begin----所有的操作都围绕列表展开，新增、更新其他比较独立 
var dataList=new Vue({
	el:"#advPlayList",
	data:{
		pageInfo : {},
		//TODO 自定义条件查询---begin--------			
		condition:{
                page:1,
                rows:customUtils.pageSize,
                id:"",




			startDate:"",
			overDate:"",
			param1:"",//province
			param2:"",//city
			param3:"",//area
			minAge:"",
			maxAge:"",
            equipmentCode:"",
            advCode:"",
			wds:[],

		    status:"",
		},
        wd1:"",
        wd2:"",
        wd3:"",
        wd4:"",
        wd5:"",
        wd6:"",
        wds:[
            {code:"advCode",name:"广告编号"},
            {code:"eqCode",name:"设备编号"},
            //{code:"cameFrom",name:"广告来源"},
            // {code:"",name:"广告类型"},
            // {code:"sex",name:"性别"},
            {code:"province",name:"省份"},
            {code:"city",name:"市"},
            {code:"area",name:"区/县"}
        ],
        province:"",
        city:"",
        area:"",

        provinces:[],
        citys:[],
        areas:[],

		dp:{
			advCode:"none",
			equipmentCode:"none",
			sex:"none",
			province:"none",
			city:"none",
			area:"none",
			//cameFrom:""
		}
      /*  dp:{
            advCode:"广告编号",
            equipmentCode:"设备编号",
            sex:"性别",
            province:"省份",
            city:"市",
            area:"区/县",
            cameFrom:""
        }*/
		,

		//自定义条件查询---end--------
		selectIds:[],//多选
		showSuccess:false,//false不显示成功提示
		selectedIdx:-1,//保存操作行的索引
		isSearch:false,//init:页面初始化；search:点击查询；paging:点击翻页；changePageSize:改变页面显示条数
		selectedPageSize:customUtils.pageSize,//修改页面数据显示条数
        num:""//页面总播放次数
	},
    // watch:{
	// 	"condition.wd1":{
     //        handler:function (newVal,oldVal) {
     //            console.log(oldVal)
     //            console.log(newVal)
     //            console.log(dataList.$data.condition.wds)
     //            if(oldVal) dataList.$data.condition.wds.add(oldVal)
     //            if(newVal) dataList.$data.condition.wds.remove(newVal)
	// 			console.log(dataList.$data.condition.wds)
     //        },
	// 		deep:true
    //
	// 	},
	// 	wd2:function (oldVal,newVal) {
    //
     //    },
	// 	wd3:function (oldVal,newVal) {
    //
     //    }
    //
    //
	// },
	mounted: function () {
	    this.getList('init');
		//初始化页面数据
        this.getAreaData();
	},
	// 在 `methods` 对象中定义方法,如果操作中函数的逻辑比较复杂，必须封装到dictType对象
	methods:{
		checkWd1:function () {
			if(dataList.$data.wd1.code == dataList.$data.wd2.code){
                dataList.$data.wd2 = "";
			}
            if(dataList.$data.wd1.code == dataList.$data.wd3.code){
                dataList.$data.wd3 = "";
            }
            if(dataList.$data.wd1.code == dataList.$data.wd4.code){
                dataList.$data.wd4 = "";
            }
            if(dataList.$data.wd1.code == dataList.$data.wd5.code){
                dataList.$data.wd5 = "";
            }
        },
        checkWd2:function () {
            if(dataList.$data.wd2.code == dataList.$data.wd1.code){
                dataList.$data.wd1 = "";
            }
            if(dataList.$data.wd2.code == dataList.$data.wd3.code){
                dataList.$data.wd3 = "";
            }
            if(dataList.$data.wd2.code == dataList.$data.wd4.code){
                dataList.$data.wd4 = "";
            }
            if(dataList.$data.wd2.code == dataList.$data.wd5.code){
                dataList.$data.wd5 = "";
            }
        },
        checkWd3:function () {
            if(dataList.$data.wd3.code == dataList.$data.wd1.code){
                dataList.$data.wd1 = "";
            }
            if(dataList.$data.wd3.code == dataList.$data.wd2.code){
                dataList.$data.wd2 = "";
            }
            if(dataList.$data.wd3.code == dataList.$data.wd4.code){
                dataList.$data.wd4 = "";
            }
            if(dataList.$data.wd3.code == dataList.$data.wd5.code){
                dataList.$data.wd5 = "";
            }
        },
        checkWd4:function () {
            if(dataList.$data.wd4.code == dataList.$data.wd1.code){
                dataList.$data.wd1 = "";
            }
            if(dataList.$data.wd4.code == dataList.$data.wd2.code){
                dataList.$data.wd2 = "";
            }
            if(dataList.$data.wd4.code == dataList.$data.wd3.code){
                dataList.$data.wd3 = "";
            }
            if(dataList.$data.wd4.code == dataList.$data.wd5.code){
                dataList.$data.wd5 = "";
            }
        },
        checkWd5:function () {
            if(dataList.$data.wd5.code == dataList.$data.wd1.code){
                dataList.$data.wd1 = "";
            }
            if(dataList.$data.wd5.code == dataList.$data.wd2.code){
                dataList.$data.wd2 = "";
            }
            if(dataList.$data.wd5.code == dataList.$data.wd3.code){
                dataList.$data.wd3 = "";
            }
            if(dataList.$data.wd5.code == dataList.$data.wd4.code){
                dataList.$data.wd4 = "";
            }
        },

	    selectChkBtn:function(e){//用于多选事件
            this.selectIds.push($(e.target).val());
        },
        changeTime:function(elem){
            this.$data.condition[elem.split("-")[0]] = $("#"+elem).val();
        },
        getAreaData:function () {
            this.$http.get("../appInfo/queryAreaTree").then(function(response){
                console.log(response);
                //alert(response.body.province);
               dataList.$data.provinces = response.body;
                console.log(dataList.$data);
            },function (res) {
            });
        },
        setCitys:function () {
            dataList.$data.citys = dataList.$data.province.citys;
            dataList.$data.city = "";
            dataList.$data.area = "";
        },
        setAreas:function () {
            dataList.$data.areas = dataList.$data.city.areas;
            dataList.$data.area = "";
        },
        //导出记录
        exportReport:function(){
            var self=this;
            var param1 = self.province?self.province.code:"";//省份
            var param2 = self.city?self.city.code:"";//城市
            var param3 = self.area?self.area.code:"";//区域
            var startDate = self.condition["startDate"];
            var overDate = self.condition["overDate"];
            var equipmentCode = self.condition["equipmentCode"];
            var wds = [];
            if (self.wd1) wds.push(self.wd1.code)
            if (self.wd2) wds.push(self.wd2.code)
            if (self.wd3) wds.push(self.wd3.code)
            if (self.wd4) wds.push(self.wd4.code)
            if (self.wd5) wds.push(self.wd5.code)
            if(true) wds.push("cameFrom")
            location.href=pageUrl+"exportReport?param1="+param1+"&param2="+param2+"&param3="+param3+"&startDate="+startDate+"&overDate="+overDate
                +"&equipmentCode="+equipmentCode+"&wds="+wds;
        },
		//搜索、分页列表
		getList:function(eventSrc){
            //dataList.pageInfo = {};
			var self=this;
			//改变页面显示条数
			if(eventSrc=="changePageSize"){
				if(this.pageInfo.pageSize != this.selectedPageSize){
					this.pageInfo.pageSize=this.selectedPageSize;
					$('#pagination').jqPaginator('destroy');
					eventSrc='init';//重新绘制控件
				}else{
					return;
				}
			}				
			//初始化时无当前页数，后台有默认第1页，每页10条
			if(this.pageInfo!=undefined && this.pageInfo.pageNum!=undefined){
				self.condition["page"]=this.pageInfo.pageNum;
				self.condition["rows"]=this.pageInfo.pageSize;
			}

			self.condition.param1 = self.province?self.province.code:"";//省份
			self.condition.param2 = self.city?self.city.code:"";//城市
			self.condition.param3  = self.area?self.area.code:"";
            self.condition.wds = [];
            if (self.wd1) self.condition.wds.push(self.wd1.code)
            if (self.wd2) self.condition.wds.push(self.wd2.code)
            if (self.wd3) self.condition.wds.push(self.wd3.code)
            if (self.wd4) self.condition.wds.push(self.wd4.code)
            if (self.wd5) self.condition.wds.push(self.wd5.code)
            self.condition.wds.push("cameFrom")
			console.log(self)
            Vue.http.options.emulateJSON = false
			//发送请求,第一个参数http对象，第二个是url，第三个是参数，第四个是成功回调，第五个是请求失败回调
			customUtils.post(self,pageListUrl,self.condition,function(response,isSuccess){
				if(isSuccess==false){
				    dataList.pageInfo.pages=0;
				    customUtils.noData('无数据',0);
				    return;
				}
				dataList.pageInfo = response.body.pagination;
                dataList.num = response.body.totalNum;
				console.log(dataList.pageInfo)
				customUtils.noData('无数据',dataList.pageInfo.pages);
				//页面初始化、搜索时，才需要执行分页控件
				if(eventSrc=='init' || eventSrc=='search'){
					customUtils.paging('#pagination',dataList.pageInfo.pages,customUtils.showPage,function(num){
						//当前页码pageNum与将要翻页的页码num不相等才执行，否则为同一页
						if(dataList.pageInfo.pageNum!=num){			    	
					    	dataList.pageInfo.pageNum=num;
							dataList.getList('paging');	
						}							    	
				    });
				}
				if(dataList.pageInfo.list.length > 0){
                    $.each(dataList.dp,function (key) {
                        var val =dataList.pageInfo.list[0][key];
                        if(val && val != null && val != ''){
                            dataList.dp[key] = 'table-cell';
                        }else{
                            dataList.dp[key] = 'none';
                        }
                    });
				}
            },function(response){
                console.log(response);
                dataList.pageInfo.pages=0;
			    customUtils.noData('无数据',0);
            });						
		},	
		//更新状态
		updateStatus:function(index){
			var record = this.pageInfo.list[index];
			var status = record.status==1?0:1;
			var param = {id:record.id,status:status};
			var self=this;
			customUtils.post(self,pageUrl + 'update',param,function(response,isSuccess){ 
				record.status=status;
				//根据需要刷新页面数据
				dataList.getList('init');
            },function(response){
                //console.log(response.body);
            });			
		},
		//初始化更新，更新对话框
		updateInit:function(index){
			//先显示窗口，才有加载效果
			this.addOrUpdatePanel("update");
			var record = this.pageInfo.list[index];
			this.selectedIdx=index;
			var url = pageUrl + 'id/'+record.id;
			var self=this;
			customUtils.get(self.$http,url,function(response,isSuccess){ 
				if(isSuccess==false){
				    return;
				}
				//回显表单数据
				$.each(response.body, function(key) {
					if(addOrUpdate[key]!==undefined && response.body[key]){
						addOrUpdate[key] = response.body[key];
					}									 
				});
            },function(response){
                console.log(response);
            });
		},
		//打开添加对话框
		addOrUpdatePanel:function(flag){
		    //清除表单数据，否则会有前一条修改数据的内容
			$.each(addOrUpdate.$data, function(key) {
				addOrUpdate[key]="";								 
			});
			var tmpTitle =  flag=="add"?"添加":"修改";
			addPanel = layer.open({
				type : 1,
				title :tmpTitle+"广告播放报表",
				area : [ '570px', 'auto' ],
				shadeClose : false, // 点击遮罩关闭
				content : $('#addPanel')
			});
		}
	}
});
//绑定列表List---end---- 

//新增或更新---------begin-------
var addOrUpdate = new Vue({
	el:"#addPanel",
	data:{
		postionCode:"",
		equipmentCode:"",
		advCode:"",
		sex:"",
		advPath:"",
		id:"",
		//以下是错误信息
		postionCodeErr:"",
		equipmentCodeErr:"",
		advCodeErr:"",
		sexErr:"",
		advPathErr:"",
	showSuccess:true,//显示成功提示
	},
	//当需要在数据变化时执行异步或开销较大的操作时，这个方式是最有用的
    watch:{
    },
	methods:{		

		save:function(){
			//逻辑校验
			if(!advPlay.checkLogic(this.$data)){
				return;
			}
			//没有id的时候表示新增，否则为更新
			var actionUrl=this.id=="" ? 'save' :'update';
			var self=this;
			customUtils.post(self,pageUrl + actionUrl,self.$data,function(response,isSuccess){ 
				if(actionUrl=='update'){
					var record = dataList.pageInfo.list[dataList.selectedIdx];
					//更新成功，刷新列表中对应的数据
					$.each(record, function(key) {
						if(addOrUpdate[key]!=undefined){
							record[key]=addOrUpdate[key];
						}									 
					});						
				}else if(isSuccess){
					dataList.getList('init');
				}
				if(isSuccess && addPanel)layer.close(addPanel);
			},function(res){
                //console.log(response.body);
            });			
		},
		//清除表单数据
		reset:function(){
			$.each(addOrUpdate.$data, function(key) {
				addOrUpdate[key]="";								 
			});
		}
	}
});
//新增或更新---------end-------
	
	
